package com.taxsystem.gaozhuanglin.invoicebusiness;

import java.util.concurrent.ExecutionException;

import org.json.JSONObject;

import com.main.taxsystem.R;
import com.taxsystem.tool.HttpUtil;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.LocalBroadcastManager;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class InquireActivity extends Activity {
	
	EditText word2;
	EditText num2;
	EditText money2;
	
	Button next2;
	Button clear2;
	
	private String m1;
	private String m2;
	private String m3;
	private String m4;
	
	private Handler handler = null;
	
	private LocalBroadcastManager localBroadcastManager = null;
	
	private final static String url = "";
	private final static String ACTION_INQUIRE = "InquireActivity.HTTP_ACTION";
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.verify_layout);
		
		word2 = (EditText)findViewById(R.id.invoice_word);
		num2 = (EditText)findViewById(R.id.invoice_num);
		money2 = (EditText)findViewById(R.id.invoice_money);
		
		next2 = (Button)findViewById(R.id.nextBtn);
		clear2 = (Button)findViewById(R.id.clearBtn);
		
		//注册Broadcast Receive
				//localBroadcastManager = LocalBroadcastManager.getInstance(InquireActivity.this);
				//localBroadcastManager.registerReceiver(new BroadcastReceiver(){
					//@Override
					//public void onReceive(Context context, Intent intent) {
						//获取JSON，并进行解析
					//}
				//}, new IntentFilter(ACTION_INQUIRE));
				
				//new MyThread3().start();
		
		handler = new Handler()
		{
			public void handleMessage(Message msg)
			{
				super.handleMessage(msg);
				if(msg.what==0x456)
				{
					Intent intent = new Intent(InquireActivity.this, InquireInformationActivity.class);
					Bundle bundle = new Bundle();
					bundle.putString("invCode", m1);
					bundle.putString("invNo", m2);
					bundle.putString("mobile", m3);
					bundle.putString("amount", m4);
					intent.putExtras(bundle);
					startActivity(intent);
					
				}
			}
		};
				
				next2.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						/*if(word2.getText().toString().equals("") || num2.getText().toString().equals("") || money2.getText().toString().equals(""))
						{
							Toast toast = new Toast(InquireActivity.this);
							toast.setGravity(Gravity.BOTTOM, 0, 0);
							toast.setText("请输入完整信息!");
							toast.setDuration(Toast.LENGTH_SHORT);
							toast.show();
						}
						else
						{
							toGetRequest();
						}*/
						Intent intent = new Intent(InquireActivity.this, InquireInformationActivity.class);
						startActivity(intent);
					}		
				});
				
				//清空内容
				clear2.setOnClickListener(new OnClickListener()
				{

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						word2.setText("");
						num2.setText("");
						money2.setText("");
					}
					
				});
	}
	
	
	protected void toGetRequest()
	{
		new Thread(new Runnable()
		{

			@Override
			public void run() {
				// TODO Auto-generated method stub
				Message m = new Message();
				m.what = 0x456;
				try {
					String result = HttpUtil.getRequest(url);
					JSONObject json = new JSONObject(result);
					m1 = json.getString("invCode");
					m2 = json.getString("invNo");
					m3 = json.getString("mobile");
					m4 = json.getString("amount");
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ExecutionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
				handler.sendMessage(m);
			}
			
		}).start();
	}
	
	/*private class MyThread3 extends Thread{

		@Override
		public void run() {
			//TODO:add your code here
			Intent intent3 = new Intent(ACTION_INQUIRE);
			try {
				String string1 = word2.getText().toString();
				String string2 = num2.getText().toString();
				String string3 = money2.getText().toString();
				
				String result = HttpUtil.getRequest(url);
				intent3.putExtra("invoice_information", result);
				localBroadcastManager.sendBroadcast(intent3);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		}
		
	}*/
}
